from application.db import get_database_connection
from application.db.mysql_db.nsfc.NsfcInfoAttachmentList import NsfcInfoAttachmentList
from application.db.mysql_db.nsfc.NsfcInfoSectionList import NsfcInfoSectionList
from application.utils.utils import generate_random_id
#
# source_id=generate_random_id("iyd")
# print(source_id)
from application.db.mysql_db.nsfc.NsfcInfoList import NsfcInfoList


@get_database_connection("default").atomic()  # 保证事务
def main():
    for item in NsfcInfoList.select():
        information_id_old = item.information_id
        item.information_id = generate_random_id('info')

        NsfcInfoSectionList.update(information_id=item.information_id).where(
            NsfcInfoSectionList.information_id == information_id_old).execute()
        NsfcInfoAttachmentList.update(information_id=item.information_id).where(
            NsfcInfoAttachmentList.information_id == information_id_old).execute()
        item.save()


if __name__ == '__main__':
    main()
